package com.online.usermanagesystem.dao;

import com.online.usermanagesystem.entity.Employee;
import com.online.usermanagesystem.utils.UpdateLangDriver;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author jhk
 */

@Mapper
public interface EmployeeMapper {

    @Select("select company_id,account_id,hire_status,name,privileges from ols_employee where id=#{id}")
    Employee query(Integer id);

    @Select("select count(*) from ols_employee where id=#{id}")
    Integer countById(Integer id);

    @Select("select count(*) from ols_employee where company_id=#{company_id} and name=#{name}")
    Integer countByName(@Param(value = "company_id") Integer companyId, String name);

    @Insert("insert into ols_employee(company_id,account_id,hire_status,name, privileges,time_create,config) values(#{company_id},#{account_id},#{hire_status},#{name},#{privileges},now(),'{}')")
    @Options(useGeneratedKeys = true)
    Integer insert(Employee employee);

    //@Update("update ols_employee set account_id=#{account_id},hire_status=#{hire_status},name=#{name},privileges=#{privileges}) where id=#{id})")
    @Update("update ols_employee(#{employee}) WHERE id = #{id}")
    @Lang(UpdateLangDriver.class)
    Integer update(Employee employee);

    @Delete("delete from ols_employee where id=#{id}")
    Integer delete(Integer id);

    @Select("select id,company_id,hire_status,name,privileges from ols_employee where account_id=#{account_id} ")
    List<Employee> queryByAccount(@Param(value = "account_id") Integer account);

    @Select("select A.id,account_id,hire_status,name,privileges from ols_employee as A,(select id from ols_company where company_id=#{company_id} limit #{index},#{count}) as B where A.id=B.id")
    List<Employee> queryByCompany(@Param(value = "company_id") Integer company, @Param(value = "index") Integer index, @Param(value = "count") Integer count);

    @Select("select A.id,account_id,hire_status,name,privileges from ols_employee as A,(select id from ols_company where company_id=#{company_id} and name like #{keyword} limit #{index},#{count}) as B where A.id=B.id")
    List<Employee> searchByCompany(@Param(value = "company_id") Integer company, @Param(value = "keyword") String keyword, @Param(value = "index") Integer index, @Param(value = "count") Integer count);
}
